#!/bin/bash

# X-minou by David GUGLIELMI <david.guglielmi@gmail.com>
# Thanks to Paco, GanGan and French users of Gentoo forum

VERSION="0.1.1.0 alpha"
FILE_SOCKET="/tmp/chat.txt"

if [ ! -e "$FILE_SOCKET" ]; then
  touch $FILE_SOCKET
  chmod a+rw $FILE_SOCKET
fi

tail -f $FILE_SOCKET 2> /dev/null &
TAILF_PID=$!

action ()
{

  echo "[0;33m($(date +%H:%M:%S))[0;32m * $USER ${1#/* }[0m" >> $FILE_SOCKET

}

write_msg ()
{
  echo "[0;33m($(date +%H:%M:%S)) [0;31m<[1;34m$USER[0;31m>[0m $1" >> $FILE_SOCKET
}

list_users ()
{
  echo "[0;32m* Users list ******************************[0;33m"
  echo ""
  ps aux | sed -n "\#[f] $FILE_SOCKET#s/ .*//p"
  echo ""
  echo "[0;32m*******************************************[0m"
}

homer ()
{

echo "[1;34m
   ___  _____
 .'/,-Y\"     \"~-.
 l.Y             ^.
 /\\               _\\_
i            ___/\"   \"\ 
|          /\"   \"\\   o !
l         ]     o !__./
 \\ _  _    \\.___./    \"~\\
  X \\/ \\            ___./
 ( \\ ___.   _..--~~\"   ~\`-.
  \` Z,--   /               \\
    \\__.  (   /       ______)
      \\   l  /-----~~\" \/  $1
       Y   \\          /
       |    \"x______.^
       |           \\
       j            Y[0m" >> $FILE_SOCKET
}

bugs ()
{

echo "[1;34m
        /\\  /\\
       |  \\ \\ \`\\
       |   \ \  \`\\
        \\   \\ \\   |
         \\   \\|   |
          \\   \\   |
           \\  |\\  |
         .-' .--. |
       /^\\  ' .-. (
       /\\    /   \\ \\
       | |   |   |  |
    .'\`|O|/  |__O|.'--.
   {   '/___,          \\
   \`. ';(_Y_) \`-'/    .'
     \\  \\\_| .'  _.-'
      \`'.\`._.'.''\`    $1
         \`-..'[0m"

}

quit_chat ()
{

  kill -9 $TAILF_PID
  action "has quit ( ${1#/* } )"
  exit 0

}

miaou ()
{

  echo "[1;34m
  (\\___/)
  (=*.*=)
  (\")__(\") < $1 >[0m
  " >> $FILE_SOCKET

}

show_help ()
{

   echo "[0;31m* available command are :
	    _: to start a line with \"/txt\"
            /? or /help: show this
            /clear: clear the chat area
	    /who or /list: show the users list
	    /me [ACTION]: display an user action
	    /erase: erase the server chat log
	    /ls: exit fast and display a fake working shell
	    /list or /l: show the current user list.
	    /bugs [MESSAGE]: Bugs Bunny say your custom message
	    /miaou [MESSAGE]: a small cat say your custom message
	    /homer [MESSAGE]: Homer say your message
	    /version: display the version that you are running
	    /quit [MESSAGE] or /exit: quit the chat room[0m"
}

trap "quit_chat \" Ctrl+C\"" TERM INT HUP

action "has joined"

while read MSG; do

 case "$MSG" in
  "/quit"* | "/q"* | "/exit"*)
   quit_chat "$MSG"
   break
   ;;
  "/clear" | "/cls")
   clear
   ;;
  "/who" | "/list" | "/l" )
   list_users
   ;;
  "/me "*)
   action "$MSG"
   ;;
  "/erase")
   echo "" > $FILE_SOCKET 1 > /dev/null 2>&1
   echo "[0;31m* log file erased[0m" 
   ;;
  "/ls")
   ls -l /dev
   quit_chat "Outch le patron"
   ;;
  "/miaou"*)
   if [ ! "${MSG#/1 }" = "/miaou" ] && [ "${MSG:6:1}" = ' ' ] ;then
    miaou "${MSG#/* }"
   else
    miaou "miaouu !"
   fi
   ;;
  "/homer"*)
   if [ ! "${MSG#/1 }" = "/homer" ] && [ "${MSG:6:1}" = ' ' ] ;then
    homer "${MSG#/* }"
   else
    homer "tOooOoOO !"
   fi
   ;;
  "/bugs"*)
   if [ ! "${MSG#/1 }" = "/homer" ] && [ "${MSG:5:1}" = ' ' ] ;then
    bugs "${MSG#/* }"
   else
    bugs "Humm quoi de neuf docteur ?"
   fi
   ;;
  "/version")
   echo "[0;31m* You are using X-Minou $VERSION[0m" 
   ;;
  "_"*)
   if [ ! "${MSG:1}" = "" ] ;then
    write_msg "${MSG:1}"
   fi
   ;;
  "/?" | "/help" | "/"*)
   show_help
   ;;
  *)
   write_msg "$MSG"
   ;;
 esac

done

quit_chat "Ctrl+D"

